package com.x.Part12_Object;


import java.util.Objects;

/**
 * ClassName: User
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.02.28 22:15
 */
public class User {
    String name;
    int age;

    public User() {
    }

    public User(String name,int age){
        this.name = name;
        this.age =  age;
    }


    //IDEA自动生成的重写的equals()
    /*
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (age != user.age) return false;
        return Objects.equals(name, user.name);
    }
    */

    //自己按照Java8重写的equals()
    @Override
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        }
        if (obj instanceof User){
            User user = (User) obj;
            if(this.age == user.age && user.name.equals(this.name)){
                return true;
            }
            else
                return false;
        }
        else
            return false;
    }

    @Override
    public String toString() {
        return "User{name:"+name+", age:"+age+"}";
    }
}
